﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;


namespace TrackingAssist.WebTrends
{
    public class WriteAtlas
    {
        private static string column_Index = "Q10";
        private static string column_Name = "Test Result-"; 

        /// <summary>
        /// integrate test result to original tracking spec
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="resultlist"></param>
        public static void MatchResulttoAtlas(string filename, ListView resultlist)
        {

            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            Excel.ApplicationClass xlsApp = new Excel.ApplicationClass();
            //xlsApp.Visible = true;
            Excel.Workbook xlsBook = null;
            Excel.Worksheet xlsSheet = null;
            string altasid = string.Empty;
            string result = string.Empty;
            try
            {
                xlsBook = xlsApp.Workbooks.Open(filename, 2, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);

                xlsSheet = (Excel.Worksheet)xlsBook.Worksheets[WebTrendsConsts.SPEC_FLAG_SHEETNAME];

                Excel.Range result_column = (Excel.Range)xlsSheet.Columns[(object)WebTrendsConsts.SPEC_Result_Column, Missing.Value];

                result_column.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, Missing.Value);

                //set the result heade
                result_column = (Excel.Range)xlsSheet.Columns[(object)WebTrendsConsts.SPEC_Result_Column, Missing.Value];
                result_column.ColumnWidth = 30;
                result_column.Cells.Font.Bold = FontStyle.Bold;
                result_column.Cells.Interior.Color = Color.LightGray.ToArgb();
                result_column.Cells.Borders.Color = Color.Black.ToArgb();

                
                result_column = xlsSheet.get_Range((object)(column_Index), Type.Missing);
                result_column.Value2 = column_Name + DateTime.Now.ToString();
                result_column.Cells.Font.Color = Color.Black.ToArgb();

                for (int i = 0; i < resultlist.Items.Count; i++)
                {
                    int imageIndex = resultlist.Items[i].ImageIndex;
                    result = ((AppIcons)imageIndex).ToString();
                    altasid = resultlist.Items[i].SubItems[1].Text.ToString();
                    if (!string.IsNullOrEmpty(altasid))
                    {
                        int n = altasid.LastIndexOf("_");
                        if (n > 0 && n < altasid.Length-1)
                        {
                            int m = Convert.ToInt16(altasid.Substring(n + 1));
                            if (m > 0)
                            {
                                Excel.Range specid = xlsSheet.get_Range((object)(WebTrendsConsts.SPEC_ID_Column + m.ToString()), Type.Missing);

                                Excel.Range row_result = xlsSheet.get_Range((object)(WebTrendsConsts.SPEC_Result_Column + m.ToString()), Type.Missing);
                                row_result.Value2 = result;
                                switch (result)
                                {
                                    case WebTrendsConsts.Result_Match:
                                        row_result.Cells.Font.Color = Color.Green.ToArgb();
                                        break;
                                    case WebTrendsConsts.Result_NotMatch:
                                        row_result.Font.Color = Color.Blue.ToArgb();
                                        break;
                                    case WebTrendsConsts.Result_Miss:
                                        row_result.Cells.Font.Color = Color.Orange.ToArgb();
                                        break;
                                    default:
                                        break;
                                }
                            }
                               
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                xlsBook.Close(false, miss, miss);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsBook);
                xlsBook = null;
                xlsApp.Quit();
                GC.Collect();
                return;

            }
            finally
            {
                xlsBook.Close(true, miss, miss);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsBook);
                xlsBook = null;
                xlsApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
                xlsApp = null;
                GC.Collect();
            }
            
        }
        public static int CopyLinksToExcel(string filename, IList<WebTrends> resultlist)
        {
            int number = 0;
            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            Excel.ApplicationClass xlsApp = new Excel.ApplicationClass();
            //xlsApp.Visible = true;
            Excel.Workbook xlsBook = null;
            Excel.Worksheet xlsSheet = null;
            try
            {
                xlsBook = xlsApp.Workbooks.Open(filename, 2, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);
                xlsSheet = (Excel.Worksheet)xlsBook.Worksheets.Add(miss, miss, miss, miss);
                xlsSheet.Name = "LinksFromFiddler";

                for (int i = 0; i < resultlist.Count; i++)
                {
                    string url = resultlist[i].FullUrl;
                    //Excel.Range range = xlsSheet.get_Range((object)"A" + i.ToString(), Type.Missing);
                    xlsSheet.Cells[i + 1, 1] = url;
                    
                    if (url != "" || url != null)
                    {
                        number++;
                    }
                }
                return number;
            }
            catch (Exception ex)
            {
                xlsBook.Close(false, miss, miss);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsBook);
                xlsBook = null;
                xlsApp.Quit();
                GC.Collect();
                return 0;
            }
            finally
            {
                xlsBook.Close(true, filename, miss);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsBook);
                xlsBook = null;
                xlsApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
                xlsApp = null;
                GC.Collect();
            }
        }
    }
}
